package saxion.speelveld.spaceengineer.view.tabs;

import java.util.Observable;
import java.util.Observer;

import saxion.speelveld.spaceengineer.GameScreen;
import saxion.speelveld.spaceengineer.R;
import saxion.speelveld.spaceengineer.controller.BitmapManager;
import saxion.speelveld.spaceengineer.controller.simpleSpinnerProducingResourcesAdapter;
import saxion.speelveld.spaceengineer.model.Building;
import saxion.speelveld.spaceengineer.model.BuildingTile;
import saxion.speelveld.spaceengineer.model.Game;
import saxion.speelveld.spaceengineer.model.InfrastructureTile;
import saxion.speelveld.spaceengineer.model.ObstacleTile;
import saxion.speelveld.spaceengineer.model.ProducingBuilding;
import saxion.speelveld.spaceengineer.model.ResourceTile;
import saxion.speelveld.spaceengineer.model.Tile;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class UpgradeScreen extends LinearLayout implements Observer
{
	// implements observer
	// in de update kijken of de game.selectedbuilding != null is. En als dat niet zo is moet
	// de informatie laten zien. Met info over het gebouw, kosten van resources wat het oplevert, etc.

	private Game game = null;

	private ImageView ivTileObjectImage;

	private TextView tvTileObjectName;

	private TextView tvTileObjectDescription;

	private TextView tvSpinnerProducingOptionsText;

	private Spinner sProducingBuildingProduceOptions;

	private Button btnSellTileContent;

	private Button btnUpgradeTileContent;

	private ProducingBuilding lastProducingBuildingClicked;

	private simpleSpinnerProducingResourcesAdapter adapter;

	private boolean forceSpinnerAdapterUpdate = false;

	public UpgradeScreen(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		init(context);
	}

	public UpgradeScreen(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init(context);
	}

	public UpgradeScreen(Context context)
	{
		super(context);
		init(context);

	}

	public void init(Context context)
	{
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.tab_tile_specs, this);

		// get all views
		ivTileObjectImage = (ImageView) findViewById(R.id.ivTileObjectImage);
		tvTileObjectName = (TextView) findViewById(R.id.tvTileObjectName);
		tvTileObjectDescription = (TextView) findViewById(R.id.tvTileObjectDescription);
		tvSpinnerProducingOptionsText = (TextView) findViewById(R.id.tvSpinnerProducingOptionsText);
		sProducingBuildingProduceOptions = (Spinner) findViewById(R.id.sProducingBuildingProduceOptions);
		btnSellTileContent = (Button) findViewById(R.id.btnSellTileContent);
		btnUpgradeTileContent = (Button) findViewById(R.id.btnUpgradeTileContent);

		btnSellTileContent.setOnClickListener(new sellTileButtonClickListener());
		btnUpgradeTileContent.setOnClickListener(new upgradeTileButtonClickListener());
		sProducingBuildingProduceOptions.setOnItemSelectedListener(new spinnerSelectedItemChangedListener());
		sProducingBuildingProduceOptions.setAdapter(null);

		// check if game != null and set view info
		if (game != null)
		{
			if (game.getSelectedTile() != null)
			{
				setFields(game.getSelectedTile());
			}
		}
		else
		{

		}
	}

	private void setFields(Tile tile)
	{
		if (tile instanceof BuildingTile)
		{
			// info
			BuildingTile buildingTile = ((BuildingTile) tile);
			Building buildingOnTile = buildingTile.getBuilding();
			ivTileObjectImage.setImageResource(buildingOnTile.getPreviewResourceImageId());
			tvTileObjectName.setText(buildingOnTile.getBuildingName());
			tvTileObjectDescription.setText(buildingOnTile.getBuildingExplanation());
			btnSellTileContent.setVisibility(View.VISIBLE);
			btnSellTileContent.setText("sell " + tvTileObjectName.getText());

			btnUpgradeTileContent.setVisibility(View.VISIBLE);

			// upgrade
			int currentBuildingLevel = buildingOnTile.getBuildingLevel();
			if (currentBuildingLevel < 3)
			{
				btnUpgradeTileContent.setEnabled(true);
				btnUpgradeTileContent.setText("upgrade to level " + (currentBuildingLevel + 1));
			}
			else
			{
				btnUpgradeTileContent.setEnabled(false);
			}

			// tilespecefieke info zetten
			if (buildingOnTile instanceof ProducingBuilding)
			{
				ProducingBuilding producingBuilding = ((ProducingBuilding) buildingOnTile);
				if (producingBuilding.getPossibleResourceProducements().size() > 1)
				{
					sProducingBuildingProduceOptions.setVisibility(View.VISIBLE);
					tvSpinnerProducingOptionsText.setVisibility(View.VISIBLE);

					if (sProducingBuildingProduceOptions.getAdapter() == null
						|| lastProducingBuildingClicked != producingBuilding)
					{
						adapter = new simpleSpinnerProducingResourcesAdapter(
							getContext(), producingBuilding.getPossibleResourceProducements());
						sProducingBuildingProduceOptions.setAdapter(adapter);
						lastProducingBuildingClicked = producingBuilding;
					}

				}
				else
				{
					sProducingBuildingProduceOptions.setAdapter(null);
					sProducingBuildingProduceOptions.setVisibility(View.GONE);
					tvSpinnerProducingOptionsText.setVisibility(View.GONE);
				}
			}
			else
			{
				sProducingBuildingProduceOptions.setVisibility(View.GONE);
				tvSpinnerProducingOptionsText.setVisibility(View.GONE);
			}
		}
		else if (tile instanceof ObstacleTile)
		{
			ObstacleTile obstacleTile = ((ObstacleTile) tile);
			int objestaclePicId = obstacleTile.getObstaclePicID();
			ivTileObjectImage.setImageResource(objestaclePicId);

			if (objestaclePicId == R.drawable.groundtilewithrock)
			{
				tvTileObjectName.setText("Rockpile");
				tvTileObjectDescription
					.setText("This rock keeps you from building something on this field. Too bad you can't destroy it.");
			}
			else if (objestaclePicId == R.drawable.cratergroundlavafull3)
			{
				tvTileObjectName.setText("Lava Crater");
				tvTileObjectDescription
					.setText("This crater filled with lava keeps you from building something on this field. Too bad you can't destroy it.");
			}
			else
			{
				tvTileObjectName.setText("Obstacle");
				tvTileObjectDescription.setText("more..");
			}

			btnSellTileContent.setVisibility(View.GONE);
			btnUpgradeTileContent.setVisibility(View.GONE);
			sProducingBuildingProduceOptions.setVisibility(View.GONE);
			tvSpinnerProducingOptionsText.setVisibility(View.GONE);
		}
		else if (tile instanceof InfrastructureTile)
		{
			InfrastructureTile infrastructureTile = ((InfrastructureTile) tile);
			ivTileObjectImage.setImageBitmap(infrastructureTile.getPic(getContext(), 32));
			tvTileObjectName.setText("infrastructure");
			// tvTileObjectDescription.setText(buildingTile.getBuilding().getBuildingExplanation());
			btnSellTileContent.setVisibility(View.VISIBLE);
			btnSellTileContent.setText("sell infrastructure");
			btnUpgradeTileContent.setVisibility(View.GONE);
			sProducingBuildingProduceOptions.setVisibility(View.GONE);
			tvSpinnerProducingOptionsText.setVisibility(View.GONE);
		}
		else if (tile instanceof ResourceTile)
		{
			ResourceTile resourceTile = ((ResourceTile) tile);
			ivTileObjectImage.setImageBitmap(BitmapManager.getTileBackgroundByResourceType(
				resourceTile.getResourceType(), getContext(), 32, 32));
			tvTileObjectName.setText(resourceTile.getResourceType().toString());
			tvTileObjectDescription.setText("If you build a mines on this resource field it will mine this resource");
			btnSellTileContent.setVisibility(View.GONE);
			btnUpgradeTileContent.setVisibility(View.GONE);
			sProducingBuildingProduceOptions.setVisibility(View.GONE);
			tvSpinnerProducingOptionsText.setVisibility(View.GONE);
		}
		else
		{
			if (tile != null)
			{
				ivTileObjectImage.setImageResource(tile.getBackgroundID());
			}
		}

	}

	public void setGameModel(Game game)
	{
		this.game = game;
		game.addObserver(this);
	}

	private class sellTileButtonClickListener implements OnClickListener
	{

		@Override
		public void onClick(View arg0)
		{
			GameScreen gs = ((GameScreen) getContext());
			gs.showSellPopup();

		}
	}

	private class spinnerSelectedItemChangedListener implements OnItemSelectedListener
	{
		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3)
		{
			if (game.getSelectedTile() instanceof BuildingTile)
			{
				Building selectedBuilding = ((BuildingTile) game.getSelectedTile()).getBuilding();

				if (selectedBuilding != null)
				{
					if (selectedBuilding instanceof ProducingBuilding)
					{
						ProducingBuilding producingBuilding = ((ProducingBuilding) selectedBuilding);
						producingBuilding.setSecondaryProducingChoice(producingBuilding
							.getPossibleResourceProducements().get(
								sProducingBuildingProduceOptions.getSelectedItemPosition()));
					}
				}
			}

		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0)
		{

		}

	}

	private class upgradeTileButtonClickListener implements OnClickListener
	{

		@Override
		public void onClick(View arg0)
		{
			GameScreen gs = ((GameScreen) getContext());
			gs.showUpgradePopup();
		}

	}

	@Override
	public void update(Observable observable, Object data)
	{
		if (game.getSelectedTile() != null)
		{
			setFields(game.getSelectedTile());
			if (game.getSelectedTile().getClass() == BuildingTile.class)
			{
				BuildingTile selTile = (BuildingTile) game.getSelectedTile();
				if (selTile.getBuilding() instanceof ProducingBuilding)
				{
					ProducingBuilding building = (ProducingBuilding) selTile.getBuilding();
					if (building.getPossibleResourceProducements().indexOf(
						building.getSecondaryProducingChoice()) != sProducingBuildingProduceOptions
						.getSelectedItemPosition())
					{
						sProducingBuildingProduceOptions
							.setSelection(building.getPossibleResourceProducements().indexOf(
								building.getSecondaryProducingChoice()));
					}
					if (adapter != null)
					{
						adapter.notifyDataSetChanged();
					}
				}
			}
		}
	}

	/*
	 * @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
	 * setContentView(R.layout.activity_upgrade_screen); }
	 * 
	 * @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar
	 * if it is present. getMenuInflater().inflate(R.menu.activity_upgrade_screen, menu); return true; }
	 */
}
